package com.project.gcsj5.common;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedMethods("GET","POST","PUT","DELETE","OPTIONS")
                //允许携带头信息
                .allowedHeaders("*")
                .allowedOrigins("*")
                .maxAge(10000);
    }
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        String uploadDir = System.getProperty("user.dir") + "/uploads/"; // 获取上传文件目录
        registry.addResourceHandler("/uploaded-files/**")
                .addResourceLocations("file:" + uploadDir);
    }
    @Override
    public void configurePathMatch(PathMatchConfigurer configurer) {
        // 优先匹配 Controller 的路径
        configurer.setUseTrailingSlashMatch(true);
    }
}
